/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package gmovil_jpa.controladores;

import gmovil_jpa.controladores.exceptions.NonexistentEntityException;
import gmovil_jpa.entidades.ModeloPlan;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import gmovil_jpa.entidades.Plan;
import gmovil_jpa.entidades.Modelo;

/**
 *
 * @author castlerock
 */
public class ModeloPlanJpaController {

    public ModeloPlanJpaController() {
        emf = Persistence.createEntityManagerFactory("gmovil_jpaPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(ModeloPlan modeloPlan) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Plan plan = modeloPlan.getPlan();
            if (plan != null) {
                plan = em.getReference(plan.getClass(), plan.getIdPlan());
                modeloPlan.setPlan(plan);
            }
            Modelo modelo = modeloPlan.getModelo();
            if (modelo != null) {
                modelo = em.getReference(modelo.getClass(), modelo.getIdModelo());
                modeloPlan.setModelo(modelo);
            }
            em.persist(modeloPlan);
            if (plan != null) {
                plan.getModeloPlanList().add(modeloPlan);
                plan = em.merge(plan);
            }
            if (modelo != null) {
                modelo.getModeloPlanList().add(modeloPlan);
                modelo = em.merge(modelo);
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(ModeloPlan modeloPlan) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            ModeloPlan persistentModeloPlan = em.find(ModeloPlan.class, modeloPlan.getIdModeloPlan());
            Plan planOld = persistentModeloPlan.getPlan();
            Plan planNew = modeloPlan.getPlan();
            Modelo modeloOld = persistentModeloPlan.getModelo();
            Modelo modeloNew = modeloPlan.getModelo();
            if (planNew != null) {
                planNew = em.getReference(planNew.getClass(), planNew.getIdPlan());
                modeloPlan.setPlan(planNew);
            }
            if (modeloNew != null) {
                modeloNew = em.getReference(modeloNew.getClass(), modeloNew.getIdModelo());
                modeloPlan.setModelo(modeloNew);
            }
            modeloPlan = em.merge(modeloPlan);
            if (planOld != null && !planOld.equals(planNew)) {
                planOld.getModeloPlanList().remove(modeloPlan);
                planOld = em.merge(planOld);
            }
            if (planNew != null && !planNew.equals(planOld)) {
                planNew.getModeloPlanList().add(modeloPlan);
                planNew = em.merge(planNew);
            }
            if (modeloOld != null && !modeloOld.equals(modeloNew)) {
                modeloOld.getModeloPlanList().remove(modeloPlan);
                modeloOld = em.merge(modeloOld);
            }
            if (modeloNew != null && !modeloNew.equals(modeloOld)) {
                modeloNew.getModeloPlanList().add(modeloPlan);
                modeloNew = em.merge(modeloNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = modeloPlan.getIdModeloPlan();
                if (findModeloPlan(id) == null) {
                    throw new NonexistentEntityException("The modeloPlan with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            ModeloPlan modeloPlan;
            try {
                modeloPlan = em.getReference(ModeloPlan.class, id);
                modeloPlan.getIdModeloPlan();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The modeloPlan with id " + id + " no longer exists.", enfe);
            }
            Plan plan = modeloPlan.getPlan();
            if (plan != null) {
                plan.getModeloPlanList().remove(modeloPlan);
                plan = em.merge(plan);
            }
            Modelo modelo = modeloPlan.getModelo();
            if (modelo != null) {
                modelo.getModeloPlanList().remove(modeloPlan);
                modelo = em.merge(modelo);
            }
            em.remove(modeloPlan);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<ModeloPlan> findModeloPlanEntities() {
        return findModeloPlanEntities(true, -1, -1);
    }

    public List<ModeloPlan> findModeloPlanEntities(int maxResults, int firstResult) {
        return findModeloPlanEntities(false, maxResults, firstResult);
    }

    private List<ModeloPlan> findModeloPlanEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(ModeloPlan.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public ModeloPlan findModeloPlan(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(ModeloPlan.class, id);
        } finally {
            em.close();
        }
    }

    public int getModeloPlanCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<ModeloPlan> rt = cq.from(ModeloPlan.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
